package o;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import com.huawei.appgallery.foundation.ui.framework.activity.BaseActivity;
import com.huawei.appmarket.support.audio.AudioBean;
import com.huawei.appmarket.support.audio.AudioPlayService;
import com.huawei.appmarket.support.audio.AudioPlayService.a;
import com.huawei.appmarket.support.audio.MainViewController;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import o.dky.j;

public class epe {
    private static SparseArray<List<AudioBean>> ˋ = new SparseArray(3);
    private static volatile epe ˏ;
    private AudioBean ʻ;
    private eoz ʼ;
    private MainViewController ʽ = new MainViewController();
    private boolean ˊ = true;
    private epd ˊॱ = new c(this);
    private List<epd> ˋॱ = new ArrayList();
    private SparseBooleanArray ˎ = new SparseBooleanArray(3);
    private WakeLock ˏॱ;
    private List<AudioBean> ͺ = new ArrayList();
    private long ॱ = -1;
    private e ॱˊ = null;
    private SparseBooleanArray ॱॱ = new SparseBooleanArray(3);
    private AudioManager ᐝ;

    static class c implements epd {
        private WeakReference<epe> ˋ;

        public c(epe o_epe) {
            this.ˋ = new WeakReference(o_epe);
        }

        public void ˊ(AudioBean audioBean) {
            if (Logger.ˎ()) {
                Logger.ˏ("AudioPlayerManager", "onStartPlay");
            }
            epe o_epe = (epe) this.ˋ.get();
            if (o_epe != null && audioBean != null) {
                epf.ˊ().ˏ(((long) audioBean.ᐝ()) + 60000);
                o_epe.ˊॱ();
                for (epd ˊ : o_epe.ʽ()) {
                    ˊ.ˊ(audioBean);
                }
            }
        }

        public void ˊ(int i) {
            if (Logger.ˎ()) {
                Logger.ˏ("AudioPlayerManager", "onPrepared");
            }
            epe o_epe = (epe) this.ˋ.get();
            if (o_epe != null) {
                for (epd ˊ : o_epe.ʽ()) {
                    ˊ.ˊ(i);
                }
            }
        }

        public void ॱ(int i, int i2) {
            epe o_epe = (epe) this.ˋ.get();
            if (o_epe != null) {
                for (epd ॱ : o_epe.ʽ()) {
                    ॱ.ॱ(i, i2);
                }
            }
        }

        public void ˋ(int i) {
            epe o_epe = (epe) this.ˋ.get();
            if (o_epe != null) {
                for (epd ˋ : o_epe.ʽ()) {
                    ˋ.ˋ(i);
                }
            }
        }

        public void ˎ() {
            if (Logger.ˎ()) {
                Logger.ˏ("AudioPlayerManager", "onPause");
            }
            epe o_epe = (epe) this.ˋ.get();
            if (o_epe != null) {
                for (epd ˎ : o_epe.ʽ()) {
                    ˎ.ˎ();
                }
                o_epe.ˋॱ();
                epf.ˊ().ˋ();
            }
        }

        public void ˋ() {
            if (Logger.ˎ()) {
                Logger.ˏ("AudioPlayerManager", "onComplete");
            }
            epe o_epe = (epe) this.ˋ.get();
            if (o_epe != null) {
                for (epd ˋ : o_epe.ʽ()) {
                    ˋ.ˋ();
                }
                o_epe.ॱˋ();
            }
        }

        public void ˋ(int i, String str) {
            if (Logger.ˎ()) {
                Logger.ˏ("AudioPlayerManager", "onError");
            }
            epe o_epe = (epe) this.ˋ.get();
            if (o_epe != null) {
                for (epd ˋ : o_epe.ʽ()) {
                    ˋ.ˋ(i, str);
                }
                o_epe.ॱˋ();
            }
        }
    }

    static class e implements ServiceConnection {
        private WeakReference<epe> ˎ;

        public e(epe o_epe) {
            this.ˎ = new WeakReference(o_epe);
        }

        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Logger.ˎ("AudioPlayerManager", "onServiceConnected");
            epe o_epe = (epe) this.ˎ.get();
            if (o_epe != null) {
                try {
                    o_epe.ʼ = ((a) iBinder).ˏ();
                    if (o_epe.ʼ != null) {
                        o_epe.ʼ.ˋ(o_epe.ˊॱ);
                        o_epe.ˊ(o_epe.ʻ);
                    }
                } catch (ClassCastException e) {
                    Logger.e("AudioPlayerManager", "onServiceConnected error! " + e.getMessage());
                }
            }
        }

        public void onServiceDisconnected(ComponentName componentName) {
            Logger.ˎ("AudioPlayerManager", "ServiceDisconnected:" + componentName);
            epe o_epe = (epe) this.ˎ.get();
            if (o_epe != null) {
                o_epe.ʼ = null;
            }
        }
    }

    private epe() {
        Context ˋ = ContentManager.getInstant().getContext();
        if (ˋ != null) {
            this.ᐝ = (AudioManager) ˋ.getSystemService("audio");
        }
    }

    public static epe ˏ() {
        if (ˏ == null) {
            synchronized (epe.class) {
                if (ˏ == null) {
                    ˏ = new epe();
                }
            }
        }
        return ˏ;
    }

    public AudioManager ˎ() {
        return this.ᐝ;
    }

    public AudioBean ॱ() {
        return this.ʻ;
    }

    public void ˎ(int i) {
        List list = (List) ˋ.get(i);
        if (list != null) {
            list.clear();
            if (this.ʻ != null && this.ʻ.ॱॱ() == i) {
                list.add(this.ʻ);
            }
        }
        this.ॱॱ.put(i, false);
    }

    public void ˊ(AudioBean audioBean) {
        if (Logger.ˎ()) {
            Logger.ˏ("AudioPlayerManager", "play audio");
        }
        Context ˋ = ContentManager.getInstant().getContext();
        if (doo.ˊ(ˋ)) {
            if (!(ॱ(audioBean) || this.ʼ == null || !this.ʻ.ˋॱ())) {
                this.ʼ.ˋ();
            }
            this.ʻ = audioBean;
            if (audioBean == null) {
                return;
            }
            if (this.ʼ == null) {
                ᐝॱ();
                return;
            }
            evr.ˊ().ॱ();
            this.ʼ.ॱ(this.ʻ);
            this.ʼ.ˎ();
            this.ˊॱ.ˊ(audioBean);
            return;
        }
        evk.ˏ(ˋ, j.ॱʼ, 0).ˋ();
        if (ʻ()) {
            ˋ();
        }
    }

    public void ˋ(AudioBean audioBean) {
        if (Logger.ˎ()) {
            Logger.ˏ("AudioPlayerManager", "pause audio");
        }
        if (ॱ(audioBean)) {
            ˋ();
        }
    }

    public void ˋ() {
        if (Logger.ˎ()) {
            Logger.ˏ("AudioPlayerManager", "pause");
        }
        if (this.ʼ != null) {
            this.ʼ.ॱ();
        }
    }

    public void ˊ(int i) {
        ʽ(i);
        ˋ();
    }

    public void ˊ() {
        if (Logger.ˎ()) {
            Logger.ˏ("AudioPlayerManager", "close");
        }
        ॱˎ();
        epn.ˎ().ˏ();
        ॱˊ();
        this.ʻ = null;
    }

    public void ˋ(int i) {
        if (Logger.ˎ()) {
            Logger.ˏ("AudioPlayerManager", "playNext");
        }
        AudioBean ʻॱ = ʻॱ();
        if (ʻॱ != null) {
            AudioBean ᐝ = ᐝ(ʻॱ);
            ᐝ.ˎ(ʻॱ.ˎ());
            ᐝ.ˎ(0);
            ᐝ.ˊ(i);
            ʽ(i);
            if (i != 1) {
                ͺ();
            }
            ˊ(ᐝ);
        }
    }

    public void ˏ(int i) {
        if (Logger.ˎ()) {
            Logger.ˏ("AudioPlayerManager", "playPrev");
        }
        AudioBean ॱᐝ = ॱᐝ();
        if (ॱᐝ != null) {
            AudioBean ᐝ = ᐝ(ॱᐝ);
            ᐝ.ˎ(ॱᐝ.ˎ());
            ᐝ.ˎ(0);
            ᐝ.ˊ(i);
            ʽ(i);
            ͺ();
            ˊ(ᐝ);
        }
    }

    public void ˋ(AudioBean audioBean, int i, boolean z) {
        if (audioBean != null) {
            audioBean.ˎ(i);
            if (z && ॱ(audioBean)) {
                this.ʼ.ˏ((long) i);
            }
        }
    }

    public boolean ॱ(AudioBean audioBean) {
        return this.ʻ != null && this.ʻ.equals(audioBean);
    }

    public boolean ʻ() {
        return this.ʻ != null && this.ʻ.ˋॱ();
    }

    public boolean ॱ(int i) {
        return this.ʻ != null && this.ʻ.ॱॱ() == i && this.ʻ.ˋॱ();
    }

    public boolean ˋ(int i, String str) {
        return this.ʻ != null && i == this.ʻ.ॱॱ() && str != null && str.equals(this.ʻ.ˏ()) && this.ʻ.ˋॱ();
    }

    public boolean ᐝ() {
        return ʻॱ() != null;
    }

    public boolean ʼ() {
        return ॱᐝ() != null;
    }

    private AudioBean ʻॱ() {
        int ˎ = ˎ(this.ʻ);
        if (ˎ < 0 || ˎ >= this.ͺ.size() - 1) {
            return null;
        }
        return (AudioBean) this.ͺ.get(ˎ + 1);
    }

    private AudioBean ॱᐝ() {
        int ˎ = ˎ(this.ʻ);
        if (ˎ <= 0 || ˎ > this.ͺ.size() - 1) {
            return null;
        }
        return (AudioBean) this.ͺ.get(ˎ - 1);
    }

    private int ˎ(AudioBean audioBean) {
        if (audioBean != null) {
            return this.ͺ.indexOf(audioBean);
        }
        return -1;
    }

    public boolean ॱॱ(int i) {
        return this.ˎ.get(i, false);
    }

    public void ˎ(int i, boolean z) {
        this.ˎ.put(i, z);
    }

    public AudioBean ˋ(String str, int i, String str2, String str3) {
        AudioBean audioBean = new AudioBean();
        audioBean.ʽ(str);
        audioBean.ˏ(i);
        audioBean.ˋ(str2);
        audioBean.ˏ(str3);
        return ᐝ(audioBean);
    }

    private AudioBean ᐝ(AudioBean audioBean) {
        int ॱॱ = audioBean.ॱॱ();
        List list = (List) ˋ.get(ॱॱ);
        if (list != null) {
            ॱॱ = list.indexOf(audioBean);
            if (ॱॱ != -1) {
                return (AudioBean) list.get(ॱॱ);
            }
        }
        list = new ArrayList();
        ˋ.put(ॱॱ, list);
        list.add(audioBean);
        return audioBean;
    }

    public void ˋ(epd o_epd) {
        if (!this.ˋॱ.contains(o_epd)) {
            this.ˋॱ.add(o_epd);
        }
    }

    public void ˊ(epd o_epd) {
        if (this.ˋॱ.contains(o_epd)) {
            this.ˋॱ.remove(o_epd);
        }
    }

    public boolean ॱॱ() {
        return this.ˊ;
    }

    public void ˎ(boolean z) {
        this.ˊ = z;
    }

    public void ˏ(AudioBean audioBean) {
        if (audioBean != null) {
            String ˏ = this.ͺ.isEmpty() ? null : ((AudioBean) this.ͺ.get(0)).ˏ();
            boolean z;
            if (ˏ == null || !ˏ.equals(audioBean.ˏ())) {
                z = false;
            } else {
                z = true;
            }
            if (!this.ॱॱ.get(audioBean.ॱॱ(), false) || !r0) {
                this.ͺ.clear();
                cfw.ˎ(new epa(audioBean.ॱॱ(), audioBean.ˊ()), new a(audioBean));
            }
        }
    }

    private void ˏ(List<AudioBean> list) {
        this.ͺ.clear();
        if (list != null) {
            AudioBean audioBean = (AudioBean) list.get(0);
            if (audioBean != null) {
                this.ॱॱ.put(audioBean.ॱॱ(), true);
            }
            this.ͺ.addAll(list);
            epn.ˎ().ˋ();
        }
    }

    private boolean ᐝॱ() {
        if (Logger.ˎ()) {
            Logger.ˏ("AudioPlayerManager", "bind to AudioPlayService");
        }
        if (this.ॱˊ == null) {
            this.ॱˊ = new e(this);
        }
        Context ˋ = ContentManager.getInstant().getContext();
        Intent intent = new Intent(ˋ, AudioPlayService.class);
        try {
            fo.ॱ(ˋ, intent);
        } catch (IllegalStateException e) {
            Logger.e("AudioPlayerManager", e.getMessage());
        }
        return ˋ.bindService(intent, this.ॱˊ, 1);
    }

    private void ॱˎ() {
        if (this.ॱˊ != null) {
            ContentManager.getInstant().getContext().unbindService(this.ॱˊ);
        }
        ContentManager.getInstant().getContext().stopService(new Intent(ContentManager.getInstant().getContext(), AudioPlayService.class));
        this.ॱˊ = null;
        this.ʼ = null;
    }

    private void ॱˋ() {
        if (ᐝ()) {
            ˋ(1);
            return;
        }
        ʽ(6);
        ॱˊ();
        ˋॱ();
        epf.ˊ().ˋ();
        epn.ˎ().ˎ(this.ʻ);
    }

    public List<epd> ʽ() {
        List<epd> arrayList = new ArrayList();
        arrayList.addAll(this.ˋॱ);
        return arrayList;
    }

    public void ˊॱ() {
        if (this.ˏॱ == null) {
            this.ˏॱ = ((PowerManager) ContentManager.getInstant().getContext().getSystemService("power")).newWakeLock(1, "AudioPlayerManager");
        }
        if (!this.ˏॱ.isHeld()) {
            this.ˏॱ.acquire();
        }
    }

    public void ˋॱ() {
        if (this.ˏॱ != null && this.ˏॱ.isHeld()) {
            this.ˏॱ.release();
        }
    }

    public void ˋ(BaseActivity baseActivity, Bundle bundle) {
        this.ʽ.ॱ(dgm.ˎ((Activity) baseActivity), baseActivity, bundle);
    }

    public void ˏ(BaseActivity baseActivity) {
        int ˎ = dgm.ˎ((Activity) baseActivity);
        ˎ(ˎ);
        ˎ(ˎ, true);
    }

    public void ʽ(int i) {
        if (this.ʼ != null) {
            this.ʼ.ˋ(i);
        }
    }

    public void ॱˊ() {
        if (this.ʻ != null && !this.ʻ.ˋ() && this.ॱ != -1) {
            long currentTimeMillis = System.currentTimeMillis() - this.ॱ;
            String valueOf = String.valueOf(this.ʻ.ॱॱ());
            if (Logger.ˎ()) {
                Logger.ˏ("AudioPlayerManager", "page stay time:" + currentTimeMillis);
            }
            dgh.ˋ("340103", evg.ˏ(currentTimeMillis, this.ʻ.ˏ(), valueOf));
            this.ॱ = -1;
        }
    }

    public long ˏॱ() {
        return this.ॱ;
    }

    public void ˋ(long j) {
        this.ॱ = j;
    }

    public boolean ˊ(int i, String str) {
        return this.ʻ != null && str != null && str.equals(this.ʻ.ˏ()) && i == this.ʻ.ॱॱ();
    }

    public void ˋ(boolean z) {
        if (this.ʻ != null) {
            this.ʻ.ˋ(z);
        }
    }

    public void ͺ() {
        Context ˋ = ContentManager.getInstant().getContext();
        if (doo.ͺ(ˋ)) {
            evk.ˏ(NetWorkUtil.getWifiResId(ˋ, j.ʻ));
        }
    }
}
